/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quizzempire.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import quizzempire.model.IModel;
import quizzempire.model.IQuizz;
import quizzempire.model.statemachine.State;

/**
 *
 * @author Victor
 */
public class EditQuizz extends javax.swing.JFrame implements Observer {

    /**
     * Creates new form EditQuizz
     */
    
    IModel model;
    IQuizz quizz;
    DefaultListModel listModel = new DefaultListModel();
    boolean loadQuizz = false;    
    
    public EditQuizz(IModel model) {
        initComponents();
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        this.model = model;
        lbLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/gamerlogo.png")));
        updateList();
        model.addObserver(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        lbLogo = new javax.swing.JLabel();
        btMainMenu = new javax.swing.JButton();
        btNextStep = new javax.swing.JButton();
        tbPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        QuizzList = new javax.swing.JList();
        btLoadQuizz = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 350));
        setPreferredSize(new java.awt.Dimension(500, 350));
        setResizable(false);

        lbLogo.setText("LOGO");

        btMainMenu.setText("MAIN MENU");
        btMainMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMainMenuActionPerformed(evt);
            }
        });

        btNextStep.setText("NEXT STEP");
        btNextStep.setMaximumSize(new java.awt.Dimension(113, 23));
        btNextStep.setMinimumSize(new java.awt.Dimension(113, 23));
        btNextStep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNextStepActionPerformed(evt);
            }
        });

        jLabel1.setText("PASSWORD");

        jLabel2.setText("SELECT A QUIZZ");

        QuizzList.setModel(listModel);
        jScrollPane1.setViewportView(QuizzList);

        btLoadQuizz.setText("LOAD");
        btLoadQuizz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLoadQuizzActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(btMainMenu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btNextStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(btLoadQuizz))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(tbPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(79, 79, 79))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tbPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btLoadQuizz)
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btMainMenu)
                            .addComponent(btNextStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btLoadQuizzActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoadQuizzActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File(model.getQuizzesFolder()));
        int retrival = chooser.showOpenDialog(this);
        if (retrival == JFileChooser.APPROVE_OPTION)
            model.loadQuizz(chooser.getSelectedFile().getAbsolutePath());
    }//GEN-LAST:event_btLoadQuizzActionPerformed

    private void btNextStepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNextStepActionPerformed
        if  (QuizzList.getSelectedIndex() == -1)
            return;
        loadQuizz=true;
        model.loadQuizzData(QuizzList.getSelectedIndex(), new String(tbPassword.getPassword()));
    }//GEN-LAST:event_btNextStepActionPerformed

    private void btMainMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMainMenuActionPerformed
        model.restart();
    }//GEN-LAST:event_btMainMenuActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList QuizzList;
    private javax.swing.JButton btLoadQuizz;
    private javax.swing.JButton btMainMenu;
    private javax.swing.JButton btNextStep;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbLogo;
    private javax.swing.JPasswordField tbPassword;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable o, Object arg) {
        State state = model.getState(); 
        if (!(state instanceof quizzempire.model.statemachine.SelectQuizzEdit))
        {
            model.deleteObserver(this);
            MainBuilder.changeScreen(this, model, state);
        }
        else
        {
            if (loadQuizz == true)
            {
                JOptionPane.showMessageDialog(this, "Invalid Password", "Invalid Password", JOptionPane.ERROR_MESSAGE);
                loadQuizz = false;
                return;
            }
            updateList();
        }
    }

    private void updateList() {
        listModel.clear();
        String[] list = model.getQuizzList();
        for (int i = 0; i < list.length; i++)
            listModel.addElement(list[i]);
    }
}
